//
//  BKResultOfMatchingScholarshipViewController.m
//  baokaodaxue
//
//  Created by SKar on 15/6/1.
//  Copyright (c) 2015年 宏图教育. All rights reserved.
//

#import "BKResultOfMatchingScholarshipViewController.h"
#import "BKMatchingScholarshipDataSource.h"
#import "BKMatchingScholarshipTableViewCell.h"
#import "BKPopoverButton.h"
#import "BKSegmentTextField.h"
#import "BKBlackTextField.h"
#import <ReactiveCocoa.h>
#import <RACEXTScope.h>
#import "BKMatchingScholarshipViewModel.h"
#import <MBProgressHUD.h>

@interface BKResultOfMatchingScholarshipViewController () <UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (weak, nonatomic) IBOutlet BKBlackTextField *pc;
@property (weak, nonatomic) IBOutlet BKBlackTextField *dq;
@property (weak, nonatomic) IBOutlet BKBlackTextField *lx;
@property (strong ,nonatomic) BKMatchingScholarshipViewModel *viewModel;
@property (strong ,nonatomic) MBProgressHUD *hud;
@end

static NSString * const cellID = @"MatchingScholarshipCell";

@implementation BKResultOfMatchingScholarshipViewController

- (instancetype)initWithNibByDataArray:(NSArray *)dataArray {
    self = [super initWithNibName:NSStringFromClass([BKResultOfMatchingScholarshipViewController class]) bundle:nil];
    if (self) {
        _dataSource = [[BKMatchingScholarshipDataSource alloc] initWithArray:dataArray cellIdentifier:cellID configureCell:^(BKMatchingScholarshipTableViewCell *cell, NSDictionary *dic) {
            [cell configureCellForDic:dic];
        }];
        _viewModel = [[BKMatchingScholarshipViewModel alloc] init];
     
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    _hud = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:_hud];
    _hud.labelText = @"正在加载";
    _hud.mode= MBProgressHUDModeIndeterminate;
    
    
    [self confirmTableView];
    @weakify(self);
    [RACObserve(self.pc, text) subscribeNext:^(id x) {
        @strongify(self);
        
        
        
        NSString *dq = nil ;
        if ([self.dq.text length]>0) {
            dq = self.dq.text;
        }
        
        NSString *fw = nil;
        if ([self.lx.text length]>0) {
            fw = self.lx.text;
        }
        if ([x isKindOfClass:[NSString class]]&&[x length]>0) {
            [self.hud show:YES];
            [self.viewModel colleageWithkq:self.kq kf:self.kf pwb:nil pwe:nil wl:nil pc:x dp:dq fw:fw jz:nil currentPage:@"1" success:^(NSArray *dataArray) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.hud hide:YES];
                   self.dataSource.dataArray = dataArray;
                    [self.tableView reloadData];
                    
                });
                
            
            } failure:^(NSString *message) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.hud hide:YES];
                    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
                    [alertView show];
                });
                

            }];
        }
    }];
    
    
    [RACObserve(self.dq, text) subscribeNext:^(id x) {
        @strongify(self);
        
        
        
        NSString *pc = nil ;
        if ([self.pc.text length]>0) {
            pc = self.pc.text;
        }
        
        NSString *fw = nil;
        if ([self.lx.text length]>0) {
            fw = self.lx.text;
        }
        if ([x isKindOfClass:[NSString class]]&&[x length]>0) {
            [self.hud show:YES];
            [self.viewModel colleageWithkq:self.kq kf:self.kf pwb:nil pwe:nil wl:nil pc:pc dp:x fw:fw jz:nil currentPage:@"1" success:^(NSArray *dataArray) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.hud hide:YES];
                    self.dataSource.dataArray = dataArray;
                    [self.tableView reloadData];
                    
                });
                
                
            } failure:^(NSString *message) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.hud hide:YES];
                    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
                    [alertView show];
                });
                
                
            }];
        }
    }];

    [RACObserve(self.lx, text) subscribeNext:^(id x) {
        @strongify(self);
        
        
        
        NSString *dq = nil ;
        if ([self.dq.text length]>0) {
            dq = self.dq.text;
        }
        
        NSString *pc = nil;
        if ([self.pc.text length]>0) {
            pc = self.pc.text;
        }
        if ([x isKindOfClass:[NSString class]]&&[x length]>0) {
            [self.hud show:YES];
            [self.viewModel colleageWithkq:self.kq kf:self.kf pwb:nil pwe:nil wl:nil pc:pc dp:dq fw:x jz:nil currentPage:@"1" success:^(NSArray *dataArray) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.hud hide:YES];
                    self.dataSource.dataArray = dataArray;
                    [self.tableView reloadData];
                    
                });
                
                
            } failure:^(NSString *message) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.hud hide:YES];
                    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
                    [alertView show];
                });
                
                
            }];
        }
    }];

    
}

- (void)dealloc {
    self.tableView.dataSource = nil;
    self.tableView.delegate = nil;
}

- (void)confirmTableView {
    [self.tableView registerNib:[BKMatchingScholarshipTableViewCell nib] forCellReuseIdentifier:cellID];
    self.tableView.dataSource = self.dataSource;
    self.tableView.delegate = self;
    _tableView.separatorColor = [UIColor clearColor];
   
    
}

- (void)setDataSource:(BKMatchingScholarshipDataSource *)dataSource {
    _dataSource = dataSource;
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
    });
}

- (IBAction)p_back:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    
    if (indexPath.row==0) {
        return 44;
    }else{
    
        NSString *text = _dataSource.dataArray[indexPath.row-1][@"biaozhun"];
        UIFont *textFont = [UIFont systemFontOfSize:16.f];
        
        
        //最大尺寸
        // MAXFLOAT 为可设置的最大高度
        CGSize size = CGSizeMake(tableView.frame.size.width/3-20, 100);
        
        //获取当前那本属性
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:textFont,NSFontAttributeName, nil];
        //实际尺寸
        CGSize actualSize = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
        
        
        CGFloat height = actualSize.height;
        return height>44.0?height+20:54;

    }
    

}
@end
